package com.aiyige.track;

import android.content.Context;

import com.aiyige.page.my.download.util.DownloadUtil;
import com.aiyige.track.annotation.Track;
import com.aiyige.utils.VideoUtil;
import com.baidu.mobstat.StatService;

import java.io.File;

/**
 * Created by zhuyakun on 2018/2/24.
 */

public class TrackUtil {
    public static final String MEDIA_SOURCE_CAMERA_SHOT = "MEDIA_SOURCE_CAMERA_SHOT";
    public static final String MEDIA_SOURCE_ALBUM = "MEDIA_SOURCE_ALBUM";
    public static final String MEDIA_SOURCE_DEFAULT = "MEDIA_SOURCE_DEFAULT";
    public static final String MEDIA_SOURCE_SUB_VIDEO_COVER = "MEDIA_SOURCE_SUB_VIDEO_COVER";
    public static final long MEDIA_TIME_OUT = 5 * 60 * 1000;


    public static void onPageStart(Context context) {
        Track track = context.getClass().getAnnotation(Track.class);
        if (track != null) {
            String pageName = track.value();
            StatService.onPageStart(context, pageName);
        }
    }

    public static void onPageEnd(Context context) {
        Track track = context.getClass().getAnnotation(Track.class);
        if (track != null) {
            String pageName = track.value();
            StatService.onPageEnd(context, pageName);
        }
    }

    public static String detectMediaSource(String mediaPath) {
        try {
            File file = new File(mediaPath);
            boolean newFile = System.currentTimeMillis() - file.lastModified() <= MEDIA_TIME_OUT;
            if (mediaPath.startsWith(VideoUtil.COVER_DIR) || mediaPath.startsWith(DownloadUtil.DOWNLOAD_DIR)) {
                if (newFile) {
                    return MEDIA_SOURCE_SUB_VIDEO_COVER;
                } else {
                    return MEDIA_SOURCE_ALBUM;
                }
            } else if (newFile) {
                return MEDIA_SOURCE_CAMERA_SHOT;
            } else {
                return MEDIA_SOURCE_ALBUM;
            }
        } catch (Exception e) {
            return MEDIA_SOURCE_DEFAULT;
        }
    }

    /**
     * time_distance=ten_days_ago(10天前)
     * time_distance=one_month_ago(1个月前)
     * time_distance=three_months_ago(3个月前)
     * time_distance=one_year_ago(1年前)
     * @param currentMillis
     * @return
     */
    public static String getHowLong(long currentMillis) {
        if (currentMillis <= 0) {
            return null;
        }
        long oneDayTime = 1000*60*60*24;
        long tenDayTime = oneDayTime*10;
        long howLongTime = System.currentTimeMillis() - currentMillis;
        // 10天以内的不做操作不返回
        if (howLongTime < tenDayTime) {
            return null;
        }
        // 大于10天小于30天
        long thirtyDaysTime = oneDayTime*30;
        if (howLongTime < thirtyDaysTime) {
            return "ten_days_ago";
        }
        // 大于30天小于3个月(3个月按90天算)
        long ninetyDaysTime = oneDayTime*90;
        if (howLongTime < ninetyDaysTime) {
            return "one_month_ago";
        }

        // 大于3个月小于1年(3个月按90天算)
        long oneYearTime = oneDayTime*365;
        if (howLongTime < oneYearTime) {
            return "three_months_ago";
        }

        return "one_year_ago";
    }
}
